﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ATW.Bus;
using BUS;
using DataAccess;
using System.Data;
using AnTeWei.Dao;
using Com.Alipay;

namespace AnTeWei.Business
{
    public class Validation
    {
        CommonHelp _commonhelp = new CommonHelp();
        public  bool Login(string strTauLoginId, string strTauPwd, ref string strError)
        {
            strError = "";

            if (VaildationDao.CheckLogin(strTauLoginId) == "0")
            {
                strError = "用户不存在";
                return false;
            }

            return true;
        }

        //注册校验
        public  bool Register(T_AppUser TAppUser,string strType, ref string strError)
        {
            strError = "";
            DBLog.WriteDBLog("AshxPortalRequest - Register", "监测T_AppUser类", TAppUser.tauType);
            DBLog.WriteDBLog("AshxPortalRequest - Register", "监测T_AppUser类", TAppUser.tauLoginId);
            DBLog.WriteDBLog("AshxPortalRequest - Register", "监测T_AppUser类", TAppUser.tauPsw);

            

            if (string.IsNullOrWhiteSpace(TAppUser.tauType))
            {
                strError = "用户类型不能为空";
                return false;
            }
            if(string.IsNullOrWhiteSpace(TAppUser.tauLoginId))
            {
                strError = "用户Id不能为空";
                return false;
            }
            if(string.IsNullOrWhiteSpace(TAppUser.tauPsw))
            {
                strError = "密码不能为空";
                return false;
            }
            //if (string.IsNullOrWhiteSpace(TAppUser.tauEmail))
            //{
            //    strError = "邮箱不能为空";
            //    return false;
            //}

            if (strType == "1")
            {
                //验证注册手机号是否存在
                if (VaildationDao.CheckLogin(TAppUser.tauLoginId) == "1")
                {
                    strError = "该手机号已注册";
                    return false;
                }
            }
            else if (strType == "2")
            {
                //验证注册手机号是否存在
                if (VaildationDao.CheckLogin(TAppUser.tauLoginId) == "0")
                {
                    strError = "该手机号未注册";
                    return false;
                }
            }
            else
            {
                strError = "操作类型不正确";
                return false;
            }

            ////验证注册手机号是否存在
            //if (CheckLoginId(TAppUser.tauLoginId)=="1")
            //{
            //    strError = "用户Id已存在";
            //    return false;
            //}

            return true;
        }

        //销售端登录
        public bool SalesLogin(string strLanId,string strTauLoginId, string strTauPwd, ref string strError)
        {
            strError = "";

            if (VaildationDao.CheckLogin(strTauLoginId) == "0")
            {
                //验证失败
                strError = _commonhelp.GetTips("6", strLanId);
                return false;
            }

            return true;
        }

        //销售端注册
        public  bool SalesRegister(string strLanId, T_AppUser TAppUser, string strType, ref string strError,ref T_AppUser entity)
        {
            try
            {
                strError = "";
                entity = TAppUser;

                #region 校验关键字段不能为空
                //if (string.IsNullOrWhiteSpace(TAppUser.tauType))
                //{
                //    //参数不能为空
                //    strError = _commonhelp.GetTips("5", strLanId);
                //    return false;
                //}
                if (string.IsNullOrWhiteSpace(TAppUser.tauLoginId))
                {
                    //参数不能为空
                    strError = _commonhelp.GetTips("5", strLanId);
                    return false;
                }
                if (string.IsNullOrWhiteSpace(TAppUser.tauPsw))
                {
                    //参数不能为空
                    strError = _commonhelp.GetTips("5", strLanId);
                    return false;
                }



                #endregion

                #region 校验手机号是否重复
                if (strType == "1")
                {
                    //验证注册手机号是否存在
                    if (VaildationDao.CheckLogin(TAppUser.tauLoginId) == "1")
                    {
                        //验证失败
                        strError = _commonhelp.GetTips("6", strLanId);
                        return false;
                    }
                }
                else if (strType == "2")
                {
                    //验证注册手机号是否存在
                    if (VaildationDao.CheckLogin(TAppUser.tauLoginId) == "0")
                    {
                        //验证失败
                        strError = _commonhelp.GetTips("6", strLanId);
                        return false;
                    }
                }
                else
                {
                    //参数错误
                    strError = _commonhelp.GetTips("4", strLanId);
                    return false;
                }
                #endregion

                if (strType != "2")
                {
                    if (string.IsNullOrWhiteSpace(TAppUser.tauAdditional2))
                    {
                        //参数不能为空
                        strError = _commonhelp.GetTips("5", strLanId);
                        return false;
                    }

                    #region 校验邀请码
                    DataTable dtSCode = new DataTable();
                    DataTable dtAUser = new DataTable();
                    //校验验证码是否存在，并获取相应数据
                    VaildationDao.SelectCodeInfo(TAppUser.tauAdditional2, ref dtSCode, ref dtAUser);
                    if (((dtSCode == null || dtSCode.Rows.Count == 0) && (dtAUser == null || dtAUser.Rows.Count == 0)) ||
                        (dtSCode.Rows.Count > 0 && dtAUser.Rows.Count > 0))
                    {
                        //验证失败
                        strError = _commonhelp.GetTips("6", strLanId);
                        return false;
                    }
                    else
                    {
                        //分供商
                        if (dtSCode.Rows.Count > 0 && (dtAUser == null || dtAUser.Rows.Count == 0))
                        {
                            TAppUser.tauType = "3";
                            return true;
                        }
                        //客户
                        else if (dtAUser.Rows.Count > 0 && (dtSCode == null || dtSCode.Rows.Count == 0))
                        {
                            Log.Debug("客户","进入邀请码计算");
                            TAppUser.tauType = "4";
                            entity.tauAdditional1 = _commonhelp.GetInviteCode("C");
                            //邀请码为客户邀请
                            if (dtAUser.Rows[0]["tauType"].ToString() == "4")
                            {
                                Log.Debug("客户", "进入邀请码计算-4");
                                //根据邀请码的自身及销售邀请码，判断该客户是几级客户:如果上级及销售邀请码一致，本身为二级客户
                                if (dtAUser.Rows[0]["tauAdditional3"] == dtAUser.Rows[0]["tauAdditional2"])
                                {
                                    entity.tauAdditional3 = dtAUser.Rows[0]["tauAdditional3"].ToString();
                                }
                                else
                                {
                                    entity.tauAdditional3 = "";
                                }

                            }
                            //邀请码为销售邀请
                            if (dtAUser.Rows[0]["tauType"].ToString() == "5")
                            {
                                Log.Debug("客户", "进入邀请码计算-5");
                                entity.tauAdditional3 = entity.tauAdditional2;
                            }
                        }
                        else
                        {
                            //参数错误
                            strError = _commonhelp.GetTips("4", strLanId);
                            return false;
                        }
                    }
                    #endregion
                }


                return true;
            }
            catch (Exception ex)
            {
                Log.Debug("SalesRegister", ex.Message.ToString());
                return false;
            }
        }

        //邀请码校验
        public  bool CheckCode(string strLanId, string strCode,ref string strError)
        {
            DataTable dtSCode=new DataTable();
            DataTable dtAUser = new DataTable();
            //校验验证码是否存在，并获取相应数据
            VaildationDao.SelectCodeInfo(strCode, ref dtSCode, ref dtAUser);
            if (((dtSCode == null || dtSCode.Rows.Count == 0) && (dtAUser == null || dtAUser.Rows.Count == 0)) ||
                (dtSCode.Rows.Count > 0 && dtAUser.Rows.Count > 0))
            {
                //验证失败
                strError = _commonhelp.GetTips("6", strLanId);
                return false;
            }
            else
            {
                return true;
            }
        }

        //更新密码校验
        public  bool UpdatePwd(HttpContext context, ref string strError)
        {
            context.Response.ContentType = "text/plain";
            strError = "";
            if (string.IsNullOrWhiteSpace(context.Request.Form["tauLoginId"]))
            {
                strError = "用户Id不能为空";
                return false;
            }
            if (string.IsNullOrWhiteSpace(context.Request.Form["NewPwd"]))
            {
                strError = "新密码不能为空";
                return false;
            }
            if (string.IsNullOrWhiteSpace(context.Request.Form["OldPwd"]))
            {
                strError = "老密码不能为空";
                return false;
            }

            return true;
        }

        //重置密码校验
        public  bool ResetPwd(HttpContext context, ref string strError)
        {
            context.Response.ContentType = "text/plain";
            strError = "";
            if (string.IsNullOrWhiteSpace(context.Request.Form["tauLoginId"]))
            {
                strError = "用户Id不能为空";
                return false;
            }
            if (string.IsNullOrWhiteSpace(context.Request.Form["NewPwd"]))
            {
                strError = "新密码不能为空";
                return false;
            }
            
            return true;
        }

        //短信验证校验
        public  bool GetMessage(HttpContext context, ref string strError)
        {
            context.Response.ContentType = "text/plain";
            strError = "";
            if (string.IsNullOrWhiteSpace(context.Request.Form["tauLoginId"]))
            {
                strError = "用户Id不能为空";
                return false;
            }
            if (string.IsNullOrWhiteSpace(context.Request.Form["OperationType"]))
            {
                strError = "验证类型不能为空";
                return false;
            }
            if (string.IsNullOrWhiteSpace(context.Request.Form["Type"]))
            {
                strError = "客户类型不能为空";
                return false;
            }
            if (context.Request.Form["OperationType"] == "1") 
            {
                //验证注册手机号是否存在
                if (VaildationDao.CheckLoginId(context.Request.Form["tauLoginId"]) == "1")
                {
                    strError = "该手机号已注册";
                    return false;
                }
            }
            else if (context.Request.Form["OperationType"] == "2")
            {
                //验证注册手机号是否存在
                if (VaildationDao.CheckLoginId(context.Request.Form["tauLoginId"]) == "0")
                {
                    strError = "该手机号未注册";
                    return false;
                }
            }
            else
            {
                strError = "操作类型不正确";
                return false;
            }
            
            return true;
        }

        //需求表头校验
        public  bool DemandInformation(T_DemandInformation TDeInfoHead, ref string strError)
        {
            //strError = "";
            //if (string.IsNullOrWhiteSpace(TDeInfoHead.tdiType))
            //{
            //    strError = "用户类型不能为空";
            //    return false;
            //}
            //if (string.IsNullOrWhiteSpace(TDeInfoHead.tauLoginId))
            //{
            //    strError = "用户Id不能为空";
            //    return false;
            //}
            //if (TDeInfoHead.TicId==null)
            //{
            //    strError = "需求类别不能为空";
            //    return false;
            //}
            //if (string.IsNullOrWhiteSpace(TDeInfoHead.tdiTitle))
            //{
            //    strError = "标题不能为空";
            //    return false;
            //}
            //if (string.IsNullOrWhiteSpace(TDeInfoHead.tdiDesc))
            //{
            //    strError = "描述不能为空";
            //    return false;
            //}
            //if (TDeInfoHead.tdiCloseTime==null)
            //{
            //    strError = "截至时间不能为空";
            //    return false;
            //}
            //if (string.IsNullOrWhiteSpace(TDeInfoHead.tdiAdditional1))
            //{
            //    strError = "交货周期不能为空";
            //    return false;
            //}
            //if (string.IsNullOrWhiteSpace(TDeInfoHead.tdiAdditional2))
            //{
            //    strError = "交货周期单位不能为空";
            //    return false;
            //}
            //if (string.IsNullOrWhiteSpace(TDeInfoHead.tdiAdditional3))
            //{
            //    strError = "付款方式不能为空";
            //    return false;
            //}
            return true;
        }

        //需求表体校验
        public  bool DemandInformation(List<T_DemandInformationDetail> TDeInfoList, ref string strError)
        {
            strError = "";
            foreach (var i in TDeInfoList)
            {
                //if (i.TdiId == null)
                //{
                //    strError = "需求表头id不能为空";
                //    return false;
                //}
                //if (i.DiId == null)
                //{
                //    strError = "子项Id不能为空";
                //    return false;
                //}
                //if ((i.tdidQty==null))
                //{
                //    strError = "需求数量不能为空";
                //    return false;
                //}
            }
            return true;
        }

        //根据模块类型查询需求
        public  bool SelectByType(HttpContext context, ref string strError)
        {
            context.Response.ContentType = "text/plain";
            strError = "";
            if (string.IsNullOrWhiteSpace(context.Request.Form["tdiType"]))
            {
                strError = "模块类型不能为空";
                return false;
            }
            if (string.IsNullOrWhiteSpace(context.Request.Form["Status"]))
            {
                strError = "状态不能为空";
                return false;
            }
            if (string.IsNullOrWhiteSpace(context.Request.Form["OrderBy"]))
            {
                strError = "排序类型不能为空";
                return false;
            }
            //if (string.IsNullOrWhiteSpace(context.Request.Form["TicId"]))
            //{
            //    strError = "需求类别不能为空";
            //    return false;
            //}
            if (string.IsNullOrWhiteSpace(context.Request.Form["CurrentPageIndex"]))
            {
                strError = "当前页不能为空";
                return false;
            }
            if (string.IsNullOrWhiteSpace(context.Request.Form["PageSize"]))
            {
                strError = "每页条数不能为空";
                return false;
            }

            return true;
        }

        //投标表头校验
        public  bool PublishBid(T_DemandInformationBid TBidHead, ref string strError)
        {
            //strError = "";
            //if (TBidHead.TdiId==null)
            //{
            //    strError = "竞标需求Id不能为空";
            //    return false;
            //}
            //if (string.IsNullOrWhiteSpace(TBidHead.tauLoginId))
            //{
            //    strError = "用户Id不能为空";
            //    return false;
            //}
            //if (TBidHead.tdibTotalPrice == null)
            //{
            //    strError = "报价不能为空";
            //    return false;
            //}
            //if (string.IsNullOrWhiteSpace(TBidHead.tdibCurrency))
            //{
            //    strError = "币种不能为空";
            //    return false;
            //}
            //if (string.IsNullOrWhiteSpace(TBidHead.tdibDeliveryPeriod))
            //{
            //    strError = "交货周期不能为空";
            //    return false;
            //}
            //if (string.IsNullOrWhiteSpace(TBidHead.tdibDeliveryPeriodUnit))
            //{
            //    strError = "交货周期单位不能为空";
            //    return false;
            //}
            //if (string.IsNullOrWhiteSpace(TBidHead.tdibPayType))
            //{
            //    strError = "付款方式不能为空";
            //    return false;
            //}
            ////校验是否超过了截止时间
            //if (!VaildationDao.CheckDiffTime(TBidHead.tdiNo.ToString()))
            //{
            //    strError = "已超过截止时间不能投标";
            //    return false;
            //}

            return true;
        }

        //投标表体校验
        public  bool PublishBid(List<T_DemandInformationBidDetail> TBidList, ref string strError)
        {
            //strError = "";
            //foreach (var i in TBidList)
            //{
            //    if (i.TdidId == null)
            //    {
            //        strError = "需求子项id不能为空";
            //        return false;
            //    }
            //    if (i.tdibdPrice == null)
            //    {
            //        strError = "子项报价不能为空";
            //        return false;
            //    }
                
            //}
            return true;
        }

        //注册校验
        public  bool SendMessage(T_Message TMessage, ref string strError)
        {
            strError = "";

            if (string.IsNullOrWhiteSpace(TMessage.tmfrom))
            {
                strError = "发送方Id不能为空";
                return false;
            }
            if (string.IsNullOrWhiteSpace(TMessage.tmto))
            {
                strError = "接收方Id不能为空";
                return false;
            }
            if (string.IsNullOrWhiteSpace(TMessage.tmMessage))
            {
                strError = "信息内容不能为空";
                return false;
            }

            return true;
        }

        //编辑供货范围
        public  bool EditSupplyRange(string strLanId, T_SupplyRange SupplyRange, ref string strError)
        {
            strError = "";

            if (string.IsNullOrWhiteSpace(SupplyRange.tauloginid))
            {
                //获取多语言提示
                strError = _commonhelp.GetTips("5", strLanId);
                return false;
            }
            if (string.IsNullOrWhiteSpace(SupplyRange.content))
            {
                //获取多语言提示
                strError = _commonhelp.GetTips("5", strLanId);
                return false;
            }
            if (string.IsNullOrWhiteSpace(SupplyRange.type))
            {
                //获取多语言提示
                strError = _commonhelp.GetTips("5", strLanId);
                return false;
            }

            return true;
        }

        //发布询价校验
        public  bool CreateRequest(string strLanId, T_Request RequestHead, List<T_RequestDetail> RequestList, ref string strError)
        {
            strError = "";

            if (string.IsNullOrWhiteSpace(RequestHead.tauloginid))
            {
                //获取多语言提示
                strError = _commonhelp.GetTips("5", strLanId);
                return false;
            }
            if (string.IsNullOrWhiteSpace(RequestHead.title))
            {
                //获取多语言提示
                strError = _commonhelp.GetTips("5", strLanId);
                return false;
            }

            //校验询价明细
            foreach (var i in RequestList)
            {
                //产品Id
                if (string.IsNullOrWhiteSpace(i.ProductionID))
                {
                    //获取多语言提示
                    strError = _commonhelp.GetTips("5", strLanId);
                    return false;
                }

                //阀体材质
                if (string.IsNullOrWhiteSpace(i.MID_B))
                {
                    //获取多语言提示
                    strError = _commonhelp.GetTips("5", strLanId);
                    return false;
                }

                //阀杆材质
                if (string.IsNullOrWhiteSpace(i.MID_S))
                {
                    //获取多语言提示
                    strError = _commonhelp.GetTips("5", strLanId);
                    return false;
                }

                //内件材质
                if (string.IsNullOrWhiteSpace(i.MID_I))
                {
                    //获取多语言提示
                    strError = _commonhelp.GetTips("5", strLanId);
                    return false;
                }

                //阀座材质
                if (string.IsNullOrWhiteSpace(i.args02))
                {
                    //获取多语言提示
                    strError = _commonhelp.GetTips("5", strLanId);
                    return false;
                }

                //口径
                if (string.IsNullOrWhiteSpace(i.SID))
                {
                    //获取多语言提示
                    strError = _commonhelp.GetTips("5", strLanId);
                    return false;
                }

                //压力等级
                if (string.IsNullOrWhiteSpace(i.PressureID))
                {
                    //获取多语言提示
                    strError = _commonhelp.GetTips("5", strLanId);
                    return false;
                }

                //连接方式
                if (string.IsNullOrWhiteSpace(i.ConnID))
                {
                    //获取多语言提示
                    strError = _commonhelp.GetTips("5", strLanId);
                    return false;
                }

                //温度
                if (string.IsNullOrWhiteSpace(i.args01))
                {
                    //获取多语言提示
                    strError = _commonhelp.GetTips("5", strLanId);
                    return false;
                }

                //公英制
                if (string.IsNullOrWhiteSpace(i.args03))
                {
                    //获取多语言提示
                    strError = _commonhelp.GetTips("5", strLanId);
                    return false;
                }

                //设计标准
                if (string.IsNullOrWhiteSpace(i.args04))
                {
                    //获取多语言提示
                    strError = _commonhelp.GetTips("5", strLanId);
                    return false;
                }

                //法兰标准
                if (string.IsNullOrWhiteSpace(i.args05))
                {
                    //获取多语言提示
                    strError = _commonhelp.GetTips("5", strLanId);
                    return false;
                }

                //结构长度标准
                if (string.IsNullOrWhiteSpace(i.args06))
                {
                    //获取多语言提示
                    strError = _commonhelp.GetTips("5", strLanId);
                    return false;
                }

                //密封测试要求
                if (string.IsNullOrWhiteSpace(i.args07))
                {
                    //获取多语言提示
                    strError = _commonhelp.GetTips("5", strLanId);
                    return false;
                }

                //1铸件，2锻件
                if (string.IsNullOrWhiteSpace(i.args08))
                {
                    //获取多语言提示
                    strError = _commonhelp.GetTips("5", strLanId);
                    return false;
                }

                //填料
                if (string.IsNullOrWhiteSpace(i.args09))
                {
                    //获取多语言提示
                    strError = _commonhelp.GetTips("5", strLanId);
                    return false;
                }

                

                //询价数量
                if (i.Query_num == null)
                {
                    //获取多语言提示
                    strError = _commonhelp.GetTips("5", strLanId);
                    return false;
                }

                //交付期
                if (i.deadLine==null)
                {
                    //获取多语言提示
                    strError = _commonhelp.GetTips("5", strLanId);
                    return false;
                }
            }


            return true;
        }

        //发布报价校验
        public  bool CreateResponse(string strLanId, List<T_ResponseDetail> ResponseList, ref string strError)
        {
            strError = "";

            //校验询价明细
            foreach (var i in ResponseList)
            {
                //报价明细Id
                if (string.IsNullOrWhiteSpace(i.ResDetailID))
                {
                    //获取多语言提示
                    strError = _commonhelp.GetTips("5", strLanId);
                    return false;
                }


                //单价
                if (i.Single_Price == null)
                {
                    //获取多语言提示
                    strError = _commonhelp.GetTips("5", strLanId);
                    return false;
                }

                //交货周期
                if (i.deliver_time == null)
                {
                    //获取多语言提示
                    strError = _commonhelp.GetTips("5", strLanId);
                    return false;
                }

                //密封测试要求
                if (string.IsNullOrWhiteSpace(i.args07))
                {
                    //获取多语言提示
                    strError = _commonhelp.GetTips("5", strLanId);
                    return false;
                }
               
            }


            return true;
        }

        //确认报价校验
        public  bool ConfirmResponse(string strLanId, List<T_ResponseDetail> ResponseList, ref string strError)
        {
            strError = "";

            //校验询价明细
            foreach (var i in ResponseList)
            {
                //报价明细Id
                if (string.IsNullOrWhiteSpace(i.ResDetailID))
                {
                    //获取多语言提示
                    strError = _commonhelp.GetTips("5", strLanId);
                    return false;
                }

                ////报价表头Id
                //if (string.IsNullOrWhiteSpace(i.ResponseID))
                //{
                //    //获取多语言提示
                //    strError = _commonhelp.GetTips("5", strLanId);
                //    return false;
                //}

                //询价明细Id
                if (string.IsNullOrWhiteSpace(i.ReqDetailID))
                {
                    //获取多语言提示
                    strError = _commonhelp.GetTips("5", strLanId);
                    return false;
                }


                //采购数量
                if (i.num == null)
                {
                    //获取多语言提示
                    strError = _commonhelp.GetTips("5", strLanId);
                    return false;
                }

            }


            return true;
        }

        //分供商提交异常
        public  bool CreateException(string strLanId, T_Exception Exception, ref string strError)
        {
            strError = "";

            if (string.IsNullOrWhiteSpace(Exception.ContractID))
            {
                //获取多语言提示
                strError = _commonhelp.GetTips("5", strLanId);
                return false;
            }
            if (string.IsNullOrWhiteSpace(Exception.type))
            {
                //获取多语言提示
                strError = _commonhelp.GetTips("5", strLanId);
                return false;
            }
            if (string.IsNullOrWhiteSpace(Exception.Content))
            {
                //获取多语言提示
                strError = _commonhelp.GetTips("5", strLanId);
                return false;
            }

            return true;
        }
    }
}